#include"vector.h"

void test1()
{
    //指定数量和数值构造v1对象
   Myvector::vector<int> v1(10,1);
   for(auto e:v1){
    cout<<e<<" ";
   }
   cout<<endl;

   //用区间进行构造v2对象
   int array[]={1,2,3,4,5};
   Myvector::vector<int> v2(array,array+5);
   for(auto e:v2){
    cout<<e<<" ";
   }
   cout<<endl;

   //用v2对象拷贝构造v3对象
   Myvector::vector<int> v3(v2);
   for(auto e:v3){
    cout<<e<<" ";
   }
   cout<<endl;

   //构造空对象v4，并将对象v1赋值给v4
   Myvector::vector<int> v4=v1;
   for(auto e:v4){
    cout<<e<<" ";
   }
   cout<<endl;
}

void test2()
{
    Myvector::vector<int> v(5,100);
    //下标+[]方式访问遍历
    for(size_t i=0;i<v.size();i++){
        cout<<v[i]<<" ";
    }
    cout<<endl;

    //迭代器遍历
    Myvector::vector<int>::iterator it=v.begin();
    while(it!=v.end()){
        cout<<*it<<" ";
        it++;
    }
    cout<<endl;
}

void test3()
{
    //创建对象v,依次插入5个字符串"vector"
    Myvector::vector<string> v;
    for(size_t i=0;i<5;i++){
        v.push_back("vector");
    }
    //输出当前的大小和容量并遍历打印
    cout<<v.size()<<" "<<v.capacity()<<endl;
    for(auto e:v){
        cout<<e<<" ";
    }
    cout<<endl;

    //调用reserve()函数扩容到8
    v.reserve(10);
    //输出当前的大小和容量
    cout<<v.size()<<" "<<v.capacity()<<endl;

    //调用resize()函数扩容到10,并将新空间中插入字符串"string"
    //输出当前的大小和容量并遍历打印
    v.resize(12,"string");
    cout<<v.size()<<" "<<v.capacity()<<endl;
    for(auto e:v){
        cout<<e<<" ";
    }
    cout<<endl;

    //调用resize()函数缩容到4，发生截断
    //输出当前的大小和容量并遍历打印
    v.resize(6);
    cout<<v.size()<<" "<<v.capacity()<<endl;
    for(auto e:v){
        cout<<e<<" ";
    }
    cout<<endl;

}

void test4()
{
    //创建对象v,依次插入5个字符串"vector"
    Myvector::vector<string> v;
    for(size_t i=0;i<5;i++){
        v.push_back("vector");
    }
    for(auto e:v){
        cout<<e<<" ";
    }
    cout<<endl;
    v.insert(v.begin()+2,"string");
    for(auto e:v){
        cout<<e<<" ";
    }
    cout<<endl;
    v.erase(v.begin()+4);
    for(auto e:v){
        cout<<e<<" ";
    }
    cout<<endl;
    
}

void test5() {
    Myvector::vector<int> vec;
    vec.push_back(0);
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);

    Myvector::vector<int>::iterator it = vec.begin();
    while (it != vec.end()) {
        if (*it % 2 == 0) {
            vec.insert(it, 5); // 在迭代器it指向的位置插入元素5
            ++it; // 由于insert可能改变底层空间，需要重新更新迭代器
        }
        ++it;
    }
    for(auto e:vec){
        cout<<e<<" ";
    }
    cout<<endl;
}

int main()
{
    //test1();
    //test2();
    //test3();
    //test4();
    test5();
    return 0;
}
